var path = require('path')
var config = require('../config.js')
var wmpInfo = config.wechat
var dataPath = config.dataPath
var async = require('async')
var fs = require('fs')
var request = require('request')
var that = this
console.log(1818199393)
// 微信卡券相关api
exports.wechat_card = {
    create: function (req, wmpname) {
        async.waterfall([
            // 获取access_token
            function (cb) {
                that.getGeneralToken(req, wmpname, function (err, result) {
                    if (err) cb(new Error(err))
                    cb(null, result.access_token)
                })
            },
            // 上传卡券logo
            function (access_token, cb) {
                var uri = ''
                cb(null)
            },
            // 设置卡券适用门店
            function (cb) {

            },
            // 选取卡券背景颜色
            function () {

            },
            // 创建卡券
            function () {

            }
        ], function (err, result) {
            if (err) console.log(err)
            console.log(result)
        })
    },
}

// 获取access_token
exports.getGeneralToken = function (req, wmpname, _cb) {
    var access_token_path = path.resolve(dataPath, wmpname + ':general_access_token.txt')
    //从文件中获取access_token
    async.waterfall([
        function (cb) {
            if (req.session[wmpname]) {
                fs.readFile(access_token_path, 'utf8', function (err, result) {
                    if (err) cb(new Error(err))
                    cb(result)
                })
            } else {
                cb(null)
            }

        },
        function (cb) {
            var _url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' + wmpInfo[wmpname].appid + '&secret=' + wmpInfo[wmpname].secret
            request(_url, function (error, response, body) {
                if (!error && response.statusCode == 200) {
                    cb(null, body)
                } else {
                    cb(new Error(error))
                }
            })
        },
        function (access_token, cb) {
            if (fs.existsSync(access_token_path)) {
                fs.unlink(access_token_path)
            }
            fs.writeFile(access_token_path, JSON.stringify(access_token), 'utf8', function (err) {
                if (err) cb(new Error(err))
                req.session.wmpname = "wvCu8cHI9U63cITq"
                cb(null, access_token)
            })

        }
    ], function (err, result) {
        _cb.call(null, err, JSON.parse(result))
    })
}
exports.getUser = function (opt, cb) {
    var _url = 'https://api.weixin.qq.com/cgi-bin/user/info?access_token=' + opt.access_token + '&openid=' + opt.openid + '&lang=zh_CN'
    request(_url, function (error, response, body) {
        cb.call(null, error, JSON.parse(body))
    })
}
exports.getCardTiket = function (req, wmpname, _cb) {
    var api_tiket_path = path.resolve(dataPath, wmpname + ':api_carTicket.txt')
    //从文件中获取access_token
    var api_ticket
    async.waterfall([
        function (cb) {
            if (req.session.apitiket) {

                fs.readFile(api_tiket_path, 'utf8', function (err, result) {
                    if (err) {
                        cb(new Error(err))
                    } else {
                        api_ticket = result
                        cb('pass')
                    }
                })
            } else {
                cb(null)
            }
        },
        function (cb) {
            that.getGeneralToken(req, wmpname, function (err, result) {
                if (err) cb(new Error(err))
                cb(null, result.access_token)
            })
        },
        function (access_token, cb) {
            var _url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=' + access_token + '&type=wx_card'
            request(_url, function (error, response, body) {
                if (!error && response.statusCode == 200) {
                    api_ticket = body
                    cb(null, body)
                } else {
                    cb(new Error(error))
                }
            })
        },
        function (api_tiket, cb) {
            if (fs.existsSync(api_tiket_path)) {
                fs.unlink(api_tiket_path)
            }
            fs.writeFile(api_tiket_path, api_tiket, 'utf8', function (err) {
                if (err) cb(new Error(err))
                req.session.apitiket = "wvCu8cHI9U63cITq"
                cb(null, api_tiket)
            })

        }
    ], function (err, result) {
        if (err === 'pass') {
            err = null
        }
        _cb.call(null, err, JSON.parse(api_ticket))
    })
}